<?php
/**
 * @Author: kendy
 * @Date:   2019-11-26 10:32:59
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-23 11:08:23
 */ 
namespace app\api\controller;
use think\Db;
use think\Request; 
use app\common\model\FriendsCircleModel as fc;
class Friendscircle extends Nocommon
{
	/**
	 * [friendsCircle 友圈文章列表]
	 * @Author   kendy
	 * @DateTime 2019-11-26T10:35:17+0800
	 * @return   [type]                   [description]
	 */
	public function friendsCircle()
	{ 
        if(Request::instance()->isPost()){
        	 $tagId = input('post.tag_id/d');
        	 $sort = input('sort','1','trim');
        	 $u_id = input('uid/d'); 
           $limit = input('limit/d');
           $page = $this->p(); 
        	 $fc = new fc();
        	 $result = $fc->getFriendsCircleList($tagId,$u_id,$page,$limit);
        	 $this->apiReturn('200','成功',$result); 
        }
	}


  /**
   * [addLikeNum 用户增加点赞]
   * @Author   kendy
   * @DateTime 2019-11-27T08:32:05+0800
   */
   public function addLikeNum()
   {
   	if(Request::instance()->isPost()){
	   	 $fc_id = input('post.id/d');
	   	 if(empty($fc_id))$this->apiReturn('400','文章id为空',0);
	   	 $u_id = input('post.uid/d');
	   	 if(empty($u_id))$this->apiReturn('400','用户信息为空',0);
        $type = input('post.type/d');
       if(empty($type))$this->apiReturn('400','点赞类型为空',0);
	   	 $fc = new fc();
	   	 $fc->addLikeNum($fc_id,$u_id,$type);
     }

   }

	/**
	 * [cancelLikeNum 取消点赞]
	 * @Author   kendy
	 * @DateTime 2019-11-27T09:19:16+0800
	 * @return   [type]                   [description]
	 */
   public function cancelLikeNum(){
   	if(Request::instance()->isPost()){
	     $fc_id = input('post.id/d');
	   	 if(empty($fc_id))$this->apiReturn('400','友圈文章id为空',0);
	   	 $u_id = input('post.uid/d');
	   	 if(empty($u_id))$this->apiReturn('400','用户信息为空',0);
         $type = input('post.type/d');
       if(empty($type))$this->apiReturn('400','点赞类型为空',0);
	   	 $fc = new fc();
	   	 $fc->cancelLikeNum($fc_id,$u_id,$type);
   	 } 
   }

   /**
    * [addFriendsCircle 增加友圈文章]
    * @Author   kendy
    * @DateTime 2019-11-27T09:45:02+0800
    */
   public function addFriendsCircle(){ 
    /**
     *  [{"status":"1","video":"http://www.baidu.com/fdser/rew234/fdsb4/89.mp4","image":"27","content":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"status":"2","video":"http://www.baidu.com/fdser/rew234/fdsb4/89.mp4","image":"21","content":"\u8f6f\u4ef6\u5de5\u7a0b"}]
     */
      if(Request::instance()->isPost()){
       $data['fc_title'] = input('post.fc_title');
       if(empty($data['fc_title']))$this->apiReturn('400','友圈标题为空',0);
       $data['fc_id'] = input('post.fc_id');
       if(empty($data['fc_id']))$this->apiReturn('400','友圈分类id为空',0);
       $data['fc_content'] = input('post.fc_content');
       $data['video'] = input('post.video');
       $data['video_content'] = input('post.video_content');
       $fc_content = json_decode($data['fc_content']); 
       $data['uid'] = input('post.uid');
       if(empty($data['uid']))$this->apiReturn('400','用户信息为空',0);
       $data['status'] = input('post.status/d');
       if(empty($data['status']))$this->apiReturn('400','友圈类型为空',0);
       $data['create_time'] = date('Y-m-d H:i:s',time());
       if(!in_array($data['status'], [1,2])){
          $this->apiReturn('400','无效的友圈类型',0);
       }
       $contentArr = [];
       $imageArr = []; 
       if($data['status'] == 1){ 
           if(!is_array($fc_content))$this->apiReturn('400','友圈内容无效',0);
           foreach ($fc_content as $key => $value) { 
            if(!empty($value->data)) $contentArr[] = $value->data;
            if(!empty($value->image)) $imageArr[] = $value->image;   
         }  
          if(count($contentArr)<0) $this->apiReturn('400','友圈图文内容为空',0);
          if(count($imageArr)<0)   $this->apiReturn('400','友圈图文图片为空',0);
       }
       if($data['status'] == 2){
        if(empty($data['video']))$this->apiReturn('400','友圈视频为空',0);
        if(empty($data['video_content']))$this->apiReturn('400','友圈视频内容为空',0);
      $data['video_covers'] = $data['video'].'?vframe/jpg/offset/1';  
      $url=$data['video']."?avinfo"; 
      $html= file_get_contents($url);
      $videoa = json_decode($html,true); 
      if($videoa['streams'][0]['duration'] < 5){ 
        $data['video_covers'] = $data['video'].'?vframe/jpg/offset/1';
      } 

       }  
       if(count($contentArr)>0)
        $data['content'] = implode(',', $contentArr); 
       if(count($imageArr)>0)
        $data['image'] = implode(',', $imageArr);  
       $fc = new fc();
       $fc->addFriendsCircle($data);
     } 
   }
  
  /**
   * [commentInfo 评论详情]
   * @Author   kendy
   * @DateTime 2019-11-28T09:22:02+0800
   * @return   [type]                   [description]
   */
  
   public function commentList()
   {  
      $fcId = input('post.f_id');
      if(empty($fcId))
        $this->apiReturn('400','友圈文章id为空',0);
      $fc = new fc();
      $fc->commentList($fcId,$page,$limit);  
   }

     
     /**
      * [friendsCircleInfo 友圈详情]
      * @Author   kendy
      * @DateTime 2019-12-06T15:01:39+0800
      * @return   [type]                   [description]
      */
  public function friendsCircleInfo()
  {
    if(Request::instance()->isPost()){
        $f_id = input('post.f_id/d');
        $uid = input('post.uid/d');
        $fc = new fc();
        $fc->getFriendsCircleInfo($f_id,$uid);
      }
    
  }

  public function getTagList()
  {
    if(Request::instance()->isPost()){ 
        $result = Db::name('fc_tags')
        ->field('id,data_name')
        ->where('is_display',0)
        ->order('data_sort desc')
        ->select();
        if(count($result)>0)
        {
            $this->apiReturn('200','返回成功',$result);
        }
     $this->apiReturn('200','返回成功',$result);
     }
  }
  /**
   * [getTourList 选择景点列表]
   * @Author   kendy
   * @DateTime 2020-01-07T13:40:42+0800
   * @return   [type]                   [description]
   */
  public function getTourList(){

     if(Request::instance()->isPost()){ 
        $result = Db::name('tours')
        ->field('tour_id,tour_name')
        ->where(['is_pass'=>1,'is_del'=>0,'is_display'=>0]) 
        ->select();
        if(count($result)>0)
        {
            $this->apiReturn('200','返回成功',$result);
        }
     $this->apiReturn('200','返回成功',$result);
     } 
  }

   
   /**
    * [getEntryList 获取词条]
    * @Author   kendy
    * @DateTime 2020-04-22T11:53:00+0800
    * @return   [type]                   [description]
    */
  public function getEntryList(){
     if(Request::instance()->isPost()){ 
        $result = Db::name('quick_comments')
        ->field('id,content')
        ->where(['is_del'=>0,'is_display'=>0]) 
        ->select();
        if(count($result)>0)
        {
            $this->apiReturn('200','返回成功',$result);
        }
     $this->apiReturn('200','返回成功',$result);
     } 
  }

}